﻿"use strict";
//var ApiServer = "http://www.$lvl.com/gas/";
var LApp = angular.module('LApp', ['leaflet-directive', 'ngLv']);

LApp.controller('RescueControl', function ($scope, $compile, $timeout, $interval, $lmapHelper, $lvl) {

    var _user = $lvl.storeItem("CurrentUser");
    window.AccessToken = _user.Token;


    angular.extend($scope, {
        TaskNo: $lvl.urlParam("taskNo"),
        BodyHeight: document.documentElement.clientHeight,
        msgContentHeight: (document.documentElement.clientHeight * 0.9 - 200) + "px",
        TaskInfo: null,
        MsgNewContent:'',
        SendMessage: function () {
            if (!$scope.MsgNewContent) {
                return;
            }
            $lvl.ajax({
                url: GlobalConfig.APIServer + 'api/keepsys/task/msgadd',
                method: 'POST',
                data: {
                    TaskNo: $scope.TaskNo,
                    MsgType: 'text',
                    MsgContent: $scope.MsgNewContent,
                    FromPlatform: 1
                },
                success: function (response) {
                    $scope.MsgNewContent = '';
                    //$scope.SelTask.MsgList.push(response.result);
                    //$scope.MessagePanelSroll();

                    //$scope.SelTask.NewContent = '';
                }
            })
        },
        SendMessageKeyUp: function (e) {
            var keycode = window.event ? e.keyCode : e.which;
            if (keycode == 13) {
                $scope.SendMessage();
            }
        },
        DealTask_1: function (taskinfo) {
            $lvl.ajax({
                url: GlobalConfig.APIServer + 'api/keepsys/task/deal',
                method: 'GET',
                data: {
                    TaskNo: taskinfo.TaskNo,
                    type: 1
                },
                success: function (response) {
                    $scope.TaskInfo.TaskState = 1;
                    //$scope.SelTask.MsgList.push(response.result);
                    //$scope.MessagePanelSroll();

                    //$scope.SelTask.NewContent = '';
                }
            })
        },
        DealTask_2: function (taskinfo) {
            $lvl.ajax({
                url: GlobalConfig.APIServer + 'api/keepsys/task/deal',
                method: 'GET',
                data: {
                    TaskNo: taskinfo.TaskNo,
                    type: 2,
                    drayId: $scope.SelectDrayUser
                },
                success: function (response) {
                    $scope.TaskInfo.TaskState = 2;
                    //$scope.SelTask.MsgList.push(response.result);
                    //$scope.MessagePanelSroll();

                    //$scope.SelTask.NewContent = '';
                }
            })
        },
        DealTask_3: function (taskinfo) {
            $lvl.ajax({
                url: GlobalConfig.APIServer + 'api/keepsys/task/deal',
                method: 'GET',
                data: {
                    TaskNo: taskinfo.TaskNo,
                    type: 5
                },
                success: function (response) {
                    //$scope.SelTask.MsgList.push(response.result);
                    //$scope.MessagePanelSroll();

                    //$scope.SelTask.NewContent = '';
                }
            })
        }
    })

    // 地图相关
    angular.extend($scope, {
        map: null,
        mapOption: {
            lng: 118.784244,
            lat: 32.041786,
            zoom: 11
        },
        mapEvent: {
            onInit: function (_map) {
                $scope.map = _map;
                $scope.InitTaskInfo();
            }
        },
        setMapCenter: function () {
            $lmapHelper.setView($scope.TaskInfo.Lng, $scope.TaskInfo.Lat, 16);
        },
        setMapUserMarker: function () {
            $lmapHelper.addImgMarker({
                lng: $scope.TaskInfo.Lng,
                lat: $scope.TaskInfo.Lat,
                icon: {
                    iconUrl: CONTENT_URL('images/mark2.png'),
                    iconSize: [80, 80],
                    iconAnchor: [40, 80],
                },
                popupOption: {
                    offset: [0, -70],
                    maxWidth: 1000,
                    autoPan: true
                },
                popup: {
                    parentScope: $scope,
                    template: 'keepcar/mapMarkerPopup.html',
                    scope: {
                        PopupTaskInfo: $scope.TaskInfo
                    }
                }
            });
        },
        setMapDrayMarker: function () {

            if (!$scope.TaskInfo.DrayLng) $scope.TaskInfo.DrayLng = 0;
            if (!$scope.TaskInfo.DrayLat) $scope.TaskInfo.DrayLat = 0;

            $scope.drayMarker = $lmapHelper.addImgMarker({
                lng: $scope.TaskInfo.DrayLng,
                lat: $scope.TaskInfo.DrayLat,
                icon: {
                    iconUrl: CONTENT_URL('images/mark3.png'),
                    iconSize: [80, 80],
                    iconAnchor: [40, 80],
                }
            });
        },
        updateMapDrayMarker: function () {
            if (!$scope.TaskInfo.DrayLng || !$scope.TaskInfo.DrayLat) return;

            if ($scope.drayMarker) {
                $scope.drayMarker.setPosition($scope.TaskInfo.DrayLng, $scope.TaskInfo.DrayLat);
            }
        },
        setMapDrayPositionTrack: function (list) {
            if (!list) return;
            if (list.length < 2) return;

            if (!$scope.drayTrackLine) {
                $scope.drayTrackLine = $lmapHelper.addLine([{ lng: 1, lat: 1 }, { lng: 2, lat: 2 }]);
            }

            var _latlngs = [];
            for (var i = 0; i < list.length; i++) {
                var item = list[i];

                if (i != 0) {
                    var _t = list[i - 1];
                    item.Distance = $lmapHelper.getDistance(item.Lng, item.Lat, _t.Lng, _t.Lat);
                    item.Distance = item.Distance.toFixed(2);

                    var dt1 = new Date(item.RecordTime);
                    var dt2 = new Date(_t.RecordTime);

                    item.Second = (dt1.valueOf() - dt2.valueOf()) / 1000;

                    item.Speed = (item.Distance / item.Second).toFixed(2);
                }
                if (item.Speed<27){
                    _latlngs.push({ lng: item.Lng, lat: item.Lat });
                }
            }

            $scope.drayTrackLine.setLatLngs(_latlngs);

            $scope.CreateTestPositionList(list);

        }
    })

    // 测试
    angular.extend($scope, {
        TestPositionEnable:false,
        TestPositionList:[],
        CreateTestPositionList: function (list) {
            for (var i = 0; i < list.length; i++) {
                var item = list[i];
                $scope.TestPositionList.push(angular.copy(item));
            }
        },
        TestPositionItemClick: function (item) {
            if (!$scope.TestPositionItemClickLine) {
                $scope.TestPositionItemClickLine = $lmapHelper.addLine([{ lng: 1, lat: 1 }, { lng: 2, lat: 2 }], {
                    options: {
                        stroke: true,
                        color: "red",
                        weight: 2,
                        opacity: 1,
                        fillOpacity: 1
                    }
                });
            }
            $scope.TestPositionItemClickLine.setLatLngs([{ lng: 0, lat: 0 }, { lng: item.Lng, lat: item.Lat }]);
        }
    })

    angular.extend($scope, {
        InitTaskInfo: function () {
            $scope.GetTaskInfo(true);
            $scope.GetTaskDrayPositionList();
        },
        //获取
        GetTaskInfo: function (isInit) {
            $lvl.get(GlobalConfig.APIServer + "api/keepsys/task/info", { TaskNo: $scope.TaskNo },
                function (response) {
                    $scope.TaskInfo = response;

                    if (isInit) {
                        $scope.setMapCenter();
                        $scope.setMapUserMarker();
                        $scope.setMapDrayMarker();
                    } else {
                        $scope.updateMapDrayMarker();
                    }

                    $timeout(function () {
                        $scope.GetTaskInfo();
                        // $scope.GetTaskDrayPositionList()
                    }, 5000);

                }, { hideloading: isInit == true ? false : true })
        },
        GetTaskDrayPositionList: function () {
            $lvl.get(GlobalConfig.APIServer + "api/keepsys/task/dray/poslist", { TaskNo: $scope.TaskNo },
                function (response) {
                    $scope.setMapDrayPositionTrack(response);
                }, { hideloading:true})
        },
        //获取板车驾驶员列表
        GetDaryDriverUserList: function () {
            $lvl.ajax({
                url: GlobalConfig.APIServer + "api/keepsys/task/draylist",
                method: 'GET',
                success: function (response) {
                    $scope.DrayDriverUserList = response;
                    $scope.SelectDrayUser = response[0].UserId.toString();
                }
            }, { hideloading: true })
        }
    })

    ////$scope.mapInit();
    //$interval($scope.GetTaskList, 2000);
    ////加载板车架驶员列表
    $scope.GetDaryDriverUserList();

    //$scope.GetTaskList();

})

